<template>
  <div class="login-container">
    <div class="login-card">
      <!-- 左侧logo区域 -->
      <div class="logo-section">
        <div class="logo">
          <img src="@/assets/images/logo.png" alt="Moolio Logo" class="logo-image" />
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="form-section">
        <div class="welcome-text">
          {{ isRegisterMode ? '创建新用户' : '欢迎来到' }}
          <div class="logo" v-if="!isRegisterMode">
            <img src="@/assets/images/logo.png" alt="Moolio Logo" class="logo-image" />
          </div>
        </div>

        <form class="login-form" @submit.prevent="handleSubmit">
          <div class="input-group">
            <input
              type="text"
              v-model="formData.username"
              placeholder="用户名/邮箱"
              class="form-input"
              :class="{ error: errors.username }"
              @input="clearError('username')"
            />
            <div class="error-message" v-if="errors.username">{{ errors.username }}</div>
          </div>

          <div class="input-group" v-if="isRegisterMode">
            <input
              type="text"
              v-model="formData.phone"
              placeholder="手机号"
              class="form-input"
              :class="{ error: errors.phone }"
              @input="clearError('phone')"
            />
            <div class="error-message" v-if="errors.phone">{{ errors.phone }}</div>
          </div>

          <div class="input-group">
            <input
              type="password"
              v-model="formData.password"
              placeholder="密码"
              class="form-input"
              :class="{ error: errors.password }"
              @input="clearError('password')"
            />
            <div class="error-message" v-if="errors.password">{{ errors.password }}</div>
          </div>

          <div class="input-group" v-if="isRegisterMode">
            <input
              type="password"
              v-model="formData.confirmPassword"
              placeholder="确认密码"
              class="form-input"
              :class="{ error: errors.confirmPassword }"
              @input="clearError('confirmPassword')"
            />
            <div class="error-message" v-if="errors.confirmPassword">{{ errors.confirmPassword }}</div>
          </div>

          <div class="form-options" v-if="!isRegisterMode">
            <label class="checkbox-label">
              <input type="checkbox" v-model="formData.rememberMe" />
              <span class="checkbox-custom"></span>
              下次自动登录
            </label>
            <a href="#" class="forgot-password">忘记密码</a>
          </div>

          <div class="form-options agreement-section" v-if="isRegisterMode">
            <label class="checkbox-label">
              <input type="checkbox" v-model="formData.agreeTerms" />
              <span class="checkbox-custom"></span>
              我已阅读并接受
              <a href="#" class="agreement-link">用户协议</a>
              和
              <a href="#" class="agreement-link">隐私政策</a>
            </label>
          </div>

          <button type="submit" class="login-button">
            {{ isRegisterMode ? '注册' : '登录' }}
          </button>

          <div class="register-link">
            {{ isRegisterMode ? '已经是会员？' : '您还没有账户？' }}
            <a href="#" class="register-text" @click.prevent="toggleMode">
              {{ isRegisterMode ? '去登录' : '去注册' }}
            </a>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const isRegisterMode = ref(false)

const formData = ref({
  username: '',
  password: '',
  phone: '',
  confirmPassword: '',
  rememberMe: false,
  agreeTerms: false,
})

const errors = ref({
  username: '',
  password: '',
  phone: '',
  confirmPassword: '',
})

const clearError = (field) => {
  if (errors.value[field]) {
    errors.value[field] = ''
  }
}

const setErrorWithTimer = (field, message) => {
  errors.value[field] = message
  // 3秒后自动清除错误信息
  setTimeout(() => {
    if (errors.value[field] === message) {
      errors.value[field] = ''
    }
  }, 3000)
}

const validateForm = () => {
  // 先清除所有错误
  errors.value = {
    username: '',
    password: '',
    phone: '',
    confirmPassword: '',
  }

  let isValid = true

  if (!formData.value.username.trim()) {
    setErrorWithTimer('username', '请输入用户名')
    isValid = false
  }

  if (isRegisterMode.value) {
    if (!formData.value.phone.trim()) {
      setErrorWithTimer('phone', '请输入手机号')
      isValid = false
    } else if (!/^1[3-9]\d{9}$/.test(formData.value.phone)) {
      setErrorWithTimer('phone', '请输入正确的手机号格式')
      isValid = false
    }

    if (!formData.value.password.trim()) {
      setErrorWithTimer('password', '请输入密码')
      isValid = false
    } else if (formData.value.password.length < 6) {
      setErrorWithTimer('password', '密码长度至少为6位')
      isValid = false
    }

    if (!formData.value.confirmPassword.trim()) {
      setErrorWithTimer('confirmPassword', '请确认密码')
      isValid = false
    } else if (formData.value.confirmPassword !== formData.value.password) {
      setErrorWithTimer('confirmPassword', '两次输入的密码不一致')
      isValid = false
    }

    if (!formData.value.agreeTerms) {
      ElMessage.warning('请阅读并接受用户协议和隐私政策')
      isValid = false
    }
  } else {
    if (!formData.value.password.trim()) {
      setErrorWithTimer('password', '请输入密码')
      isValid = false
    }
  }

  return isValid
}

const handleSubmit = () => {
  if (!validateForm()) {
    return
  }

  if (isRegisterMode.value) {
    ElMessage.success('注册信息已提交')
    console.log('注册数据:', {
      username: formData.value.username,
      password: formData.value.password,
      phone: formData.value.phone,
    })
    // 这里添加注册逻辑
  } else {
    ElMessage.success('登录信息已提交')
    console.log('登录数据:', {
      username: formData.value.username,
      password: formData.value.password,
      rememberMe: formData.value.rememberMe,
    })
    // 这里添加登录逻辑
  }
}

const toggleMode = () => {
  isRegisterMode.value = !isRegisterMode.value
  // 清除错误信息
  errors.value = {
    username: '',
    password: '',
    phone: '',
    confirmPassword: '',
  }
  // 重置表单
  formData.value = {
    username: '',
    password: '',
    phone: '',
    confirmPassword: '',
    rememberMe: false,
    agreeTerms: false,
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background-image: url('@/assets/images/background.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}

.login-card {
  background: rgba(255, 255, 255, 0.6);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 100%;
  max-width: 900px;
  min-height: 500px;
  display: flex;
}

.logo-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.logo {
  text-align: center;
}

.logo-image {
  max-width: 200px;
  max-height: 150px;
  width: auto;
  height: auto;
}

.form-section {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}

.form-section::before {
  content: '';
  position: absolute;
  top: 30px;
  left: 30px;
  right: 30px;
  bottom: 30px;
  background: white;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  z-index: 1;
}

.form-section > * {
  position: relative;
  z-index: 2;
}

.welcome-text {
  font-size: 20px;
  color: #333;
  margin-bottom: 40px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.welcome-text .logo {
  display: inline-flex;
  align-items: center;
}

.welcome-text .logo-image {
  height: 24px;
  width: auto;
  max-width: 120px;
}

.login-form {
  width: 100%;
}

.input-group {
  margin-bottom: 20px;
}

.form-input {
  width: 100%;
  padding: 15px 20px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s;
  box-sizing: border-box;
}

.form-input:focus {
  border-color: #999999;
  background-color: #f4f4f4;
  outline: none;
}

.form-input::placeholder {
  color: #999;
}

.form-input.error {
  border-color: #f070b0;
}

.error-message {
  color: #f070b0;
  font-size: 12px;
  margin-top: 5px;
  margin-left: 5px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  font-size: 14px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #666;
}

.checkbox-label input[type='checkbox'] {
  display: none;
}

.checkbox-custom {
  width: 16px;
  height: 16px;
  border: 1px solid #ddd;
  border-radius: 3px;
  margin-right: 8px;
  position: relative;
  transition: all 0.3s;
}

.checkbox-label input[type='checkbox']:checked + .checkbox-custom {
  background: #eb4196;
  border-color: #eb4196;
}

.checkbox-label input[type='checkbox']:checked + .checkbox-custom::after {
  content: '✓';
  position: absolute;
  color: white;
  font-size: 12px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.forgot-password {
  color: #eb4196;
  text-decoration: none;
  font-size: 14px;
}

.forgot-password:hover {
  text-decoration: underline;
}

.login-button {
  width: 100%;
  background: #eb4196;
  color: white;
  border: none;
  padding: 15px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-bottom: 20px;
}

.login-button:hover {
  background: #d63384;
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.register-text {
  color: #eb4196;
  text-decoration: none;
  margin-left: 5px;
}

.register-text:hover {
  text-decoration: underline;
}

.agreement-section {
  justify-content: flex-start;
  flex-wrap: wrap;
}

.agreement-section .checkbox-label {
  line-height: 1.4;
}

.agreement-link {
  color: #eb4196;
  text-decoration: none;
  margin: 0 2px;
}

.agreement-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    flex-direction: column;
    max-width: 400px;
  }

  .logo-section {
    padding: 30px 20px;
  }

  .logo-image {
    max-width: 150px;
    max-height: 100px;
  }

  .form-section {
    padding: 30px 20px;
  }

  .form-section::before {
    top: 15px;
    left: 15px;
    right: 15px;
    bottom: 15px;
    border-radius: 12px;
  }

  .welcome-text {
    font-size: 18px;
    margin-bottom: 30px;
  }
}
</style>
